﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BaseHelper;

namespace DeviceSDK.LED.ShiZhan
{
    public class ShiZhan_2010 
    {
        private static bool IsConnected;
        private const int WM_LED_NOTIFY = 1025;
        private static int dev = -1;
        private static int eAddr = 0;
        private static int ret = -999;
        private static ShiZhan_SDK Sdk = new ShiZhan_SDK();
        private delegate void DefWndProcHanlder();
        private event DefWndProcHanlder DefWndProcEvent;
        private static ShiZhan_Msg msg;
        private static bool Sending=false;
        private static object IsBusying = false;

        //获取错误代码
        public static string Get_Error(int ret)
        {

            switch (ret)
            {
                case 0:
                    return "操作成功！";
                case 1:
                    return "操作失败！";
                case 2:
                    return "命令执行超时！";
                case -1:
                    return "本设备不支持此命令！";
                default:
                    return "未知错误！";
            }
        }

        //protected override void DefWndProc(ref System.Windows.Forms.Message m)
        //{
        //    switch (m.Msg)
        //    {
        //        case WM_LED_NOTIFY:
        //            OnLEDNotify((UInt32)m.Msg, (UInt32)m.WParam, (UInt32)m.LParam);
        //            break;
        //        default:
        //            //base.DefWndProc(ref m);//调用基类函数处理非自定义消息。
        //            break;
        //    }
        //}
        
        public static void OnLEDNotify(UInt32 msg, UInt32 wParam, UInt32 lParam)
        {
            ret = -1;
            ShiZhan_SDK.NotifyMessage notify = new ShiZhan_SDK.NotifyMessage();
            Sdk.LED_GetDeviceNotifyMessage(dev, ref notify);
            //LEDSender.LED_GetNotifyMessage(ref notify);
            if (notify.Message == 7)
                Sending = true;
            else
                Sending = false;

            switch (notify.Message)
            {
                case (int)ShiZhan_SDK.eResponseMessage.LM_TX_COMPLETE:
                    ret = 0;
                    break;
                case (int)ShiZhan_SDK.eResponseMessage.LM_TIMEOUT:
                    ret = 2;
                    break;
                case (int)ShiZhan_SDK.eResponseMessage.LM_RESPOND:
                    switch (notify.Command)
                    {
                        case ShiZhan_SDK.PKC_GET_POWER:
                            ret = notify.Status == 1 ? 0 : 1;
                            break;
                        case ShiZhan_SDK.PKC_SET_POWER:
                            ret = 0;
                            break;
                        case ShiZhan_SDK.PKC_GET_BRIGHT:
                            ret = notify.Status;
                            break;
                        case ShiZhan_SDK.PKC_GET_LEAF:
                        case ShiZhan_SDK.PKC_SET_LEAF:
                            ret = notify.Status;
                            break;
                        case ShiZhan_SDK.PKC_SET_BRIGHT:
                            ret = 1;
                            break;
                        case ShiZhan_SDK.PKC_ADJUST_TIME:
                            ret = 1;
                            break;
                        case ShiZhan_SDK.PKC_SET_AUTO_POWER:
                            ret = 1;
                            break;
                    }
                    break;
            }


        }

        private static int WaitReturn()
        {
            int TimeOut =50;
            while (ret == -999 && TimeOut-- > 0 || Sending)
            {
                Thread.Sleep(100);
                Application.DoEvents();
            }
            if (ret == -999 && TimeOut < 0)
                ret = 2;
            Close();
            return ret;
        }

        public static void Init()
        {
            Sending = false;
            ret = -999;
            if (LEDHelper.Release_IsOrderToSend)
            {
                Random rnd = new Random();
                while ((bool)IsBusying)
                    Thread.Sleep(rnd.Next(1000));
                lock (IsBusying)
                {
                    IsBusying = true;
                }
            }
            if (!IsConnected)
            {
                try
                {
                    Sdk.LED_CreateReportServer(8888);

                    ShiZhan_SDK.DEVICEPARAM param = new ShiZhan_SDK.DEVICEPARAM();
                    param.devType = (uint)ShiZhan_SDK.eDevType.DEV_UDP;
                    param.locPort = 8881;
                    param.rmtPort = 6666;
                    msg = new ShiZhan_Msg();

                    dev = Sdk.LED_Open(ref param, ShiZhan_SDK.NOTIFY_EVENT, msg.GetHandle, WM_LED_NOTIFY);

                    IsConnected = true;
                }
                catch (Exception ex)
                {
                    LogHelper.Err("RT.Helper.Device.LED.ShiZhan.ShiZhan_2010.cs", "Init()", ex);
                    IsBusying = false;
                    ret = 1;
                    Close();
                }                                                                                                                                                                                                                                               
            }
        }

        public static void Close()
        {
           
            try
            {
                Sdk.LED_ReleaseReportServer();
                Sdk.LED_CloseAll();
            }
            catch (Exception ex)
            {
                LogHelper.Err("RT.Helper.Device.LED.ShiZhan.ShiZhan_2010.cs", "Close()", ex);
            }
            finally
            {
                IsConnected = false;
                IsBusying = false;
            }
        }

        //获得-屏幕状态
        public static int Get_Screen_Status(string IP, ref int Value)
        {
            Init();
            ret = -999;
            Sdk.LED_GetPower(dev, (byte)Convert.ToInt16(eAddr), IP, 6666);
            return WaitReturn();
        }

        //设置-屏幕-关闭
        public static int Set_Screen_Close(string IP)
        {
            Init();
            ret = -999;
            Sdk.LED_SetPower(dev, (byte)Convert.ToInt16(eAddr), IP, 6666, ShiZhan_SDK.ePowerStatus.LED_POWER_OFF);
            return WaitReturn();
        }

        //设置-屏幕-开启
        public static int Set_Screen_Open(string IP)
        {
            Init();
            ret = -999;
            Sdk.LED_SetPower(dev, (byte)Convert.ToInt16(eAddr), IP, 6666, ShiZhan_SDK.ePowerStatus.LED_POWER_ON);
            return WaitReturn();
        }

        //获得-屏幕亮度
        public static int Get_Brightness(string IP, ref int Value)
        {
            Init();
            ret = -999;
            Sdk.LED_GetBrightness(dev, (byte)Convert.ToInt16(eAddr), IP, 6666);
            return WaitReturn();
        }

        //设置-屏幕亮度
        public static int Set_Brightness(string IP, int Value)
        {
            Init();
            ret = -999;
            Sdk.LED_SetBrightness(dev, (byte)Convert.ToInt16(eAddr), IP, 6666, Value);
            return WaitReturn();
        }

        //获得-音量
        public static int Get_Volume(string IP, ref int Value)
        {
            return -1;
        }

        //设置-音量
        public static int Set_Volume(string IP, int Value)
        {
            return -1;
        }

        //设置-时间
        public static int Set_Time(string IP)
        {
            Init();
            ret = -999;
            Sdk.LED_AdjustTime(dev, (byte)Convert.ToInt16(eAddr), IP, 6666);

            return WaitReturn();
        }

        //发送-节目
        public static int Send_Programs(string IP, PlaybillProgramInfo[] Programs)
        {
            Init();
            //这里需要连接，需要open
            
            Sdk.MakeRoot(ShiZhan_SDK.eRootType.ROOT_PLAY, ShiZhan_SDK.eScreenType.SCREEN_COLOR);
                
            foreach (PlaybillProgramInfo pinfo in Programs)
            {
                pinfo.IsEffectRand = pinfo.IsEffectRand == 0 ? 0 : -1;
                switch (pinfo.Type)
                {
                    case "picture":
                        SZ_AddPictureArea(pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height, pinfo.IsEffectRand, pinfo.EffectTime, pinfo.PlayTime, pinfo.FilePath);
                        break;

                    case "video":
                        //LX_AddVideoArea(0, "", 0, true, pinfo.ScreenAreaID, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height, "", pinfo.ID, pinfo.FilePath, 0, 0, 0, 0, 0, 0, 1, IP);
                        break;

                    case "rtf":
                        //LX_AddTextArea(0, "", 0, true, pinfo.ScreenAreaID, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height, "", 0, 0, 0, 0, pinfo.ID, pinfo.FilePath, pinfo.Affect, 1, pinfo.PlayTime, 1, 1, 0, IP);
                        break;

                    case "text":
                        SZ_AddStringTxtArea(pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height, pinfo.IsEffectRand, pinfo.EffectTime, pinfo.PlayTime, pinfo.Text, pinfo.FontName, pinfo.FontSize, pinfo.FontColor);
                        break;
                }
            }

            ret = -999;

            Sdk.LED_SendToScreen(dev, (byte)Convert.ToInt16(eAddr), IP, 6666);

            return WaitReturn();
        }

        private static void SZ_AddStringTxtArea(int X, int Y, int Width, int Height, int Affect, int AffectTime, int PlayTime, string Text, string FontName, int FontSize, string FontColor)
        {
            ShiZhan_SDK.RECT r;
            r.left = X;
            r.top = Y;
            r.right = X + Width;
            r.bottom = Y + Height;


            Sdk.AddLeaf(PlayTime*1000);
            //Sdk.AddTextEx(Text, ref r, 1, 1, 1, "宋体",12, 255, 0, 1);
            Sdk.AddTextEx(Text, ref r, Affect+1, AffectTime+1, 1, FontName, FontSize, 255, 0, 1);
        }


        private static void SZ_AddPictureArea(int X, int Y, int Width, int Height, int Affect, int AffectTime, int PlayTime, string FilePath)
        {
            ShiZhan_SDK.RECT r = new ShiZhan_SDK.RECT();
            r.left = X;
            r.top = Y;
            r.right = X + Width;
            r.bottom = Y + Height;
            Sdk.AddLeaf(PlayTime*1000);
           // Sdk.AddPicture(FilePath, ref r, Affect, AffectTime, 1, 0);
            Sdk.AddPicture(FilePath, ref r, 1, 1, 1, 1);
        }
    }

}
